{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Applies the watershed filter to an image, i.e., separating it in nearly homogeneous regions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "medpy_watershed.py resources/b0.nii.gz output/watershed.nii.gz"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Looking at our result we get\n",
    "<table>\n",
    "<tr>\n",
    "<td><img alt=\"Original image\" src=\"images/b0.png\" width=\"200\" /></td>\n",
    "<td><img alt=\"Region image\" src=\"images/watershed_fail.png\" width=\"200\" /></td>\n",
    "</tr>\n",
    "<tr>\n",
    "<td>Original image</td>\n",
    "<td>Region image</td>\n",
    "</tr>\n",
    "</table>\n",
    "That is not quite what we expected. If your resulting imageshows this gradient-like transition of gray values from one to the other site, it means that too many regions where created. In fact, this can result in that many regions, that the numeric type overflows.\n",
    "\n",
    "To fix this, let's first take a look at the script's parameters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "usage: medpy_watershed.py [-h] [--mindist MINDIST] [--mask MASK] [-v] [-d]\n",
      "                          [-f]\n",
      "                          input output\n",
      "\n",
      "Applies the watershed segmentation an image using the supplied parameters.\n",
      "Note that this version does not take the voxel-spacing into account. If you\n",
      "require this function, please take a look at the medpy_itk_watershed.py\n",
      "script. Copyright (C) 2013 Oskar Maier This program comes with ABSOLUTELY NO\n",
      "WARRANTY; This is free software, and you are welcome to redistribute it under\n",
      "certain conditions; see the LICENSE file or <http://www.gnu.org/licenses/> for\n",
      "details.\n",
      "\n",
      "positional arguments:\n",
      "  input              Source volume (usually a gradient image).\n",
      "  output             Target volume.\n",
      "\n",
      "optional arguments:\n",
      "  -h, --help         show this help message and exit\n",
      "  --mindist MINDIST  The minimum distance between local minima in voxel units.\n",
      "  --mask MASK        Optional binary mask image denoting the area over which\n",
      "                     to compute the watershed.\n",
      "  -v                 Display more information.\n",
      "  -d                 Display debug information.\n",
      "  -f                 Silently override existing output images.\n"
     ]
    }
   ],
   "source": [
    "medpy_watershed.py -h"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note the MINDIST value. With this, the minimal distance between to local minima can be set and thus the minimal size of the regions roughly controlled. let's try it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "medpy_watershed.py resources/b0.nii.gz output/watershed.nii.gz --mindist 10 -f"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<table>\n",
    "<tr>\n",
    "<td><img alt=\"Original image\" src=\"images/b0.png\" width=\"200\" /></td>\n",
    "<td><img alt=\"Region image\" src=\"images/watershed.png\" width=\"200\" /></td>\n",
    "<td><img alt=\"Region image (colored)\" src=\"images/watershed_colored.png\" width=\"200\" /></td>\n",
    "</tr>\n",
    "<tr>\n",
    "<td>Original image</td>\n",
    "<td>Region image</td>\n",
    "<td>Region image (colored)</td>\n",
    "</tr>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Better. In the colored version, the brain becomes visible."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Bash",
   "language": "bash",
   "name": "bash"
  },
  "language_info": {
   "codemirror_mode": "shell",
   "file_extension": ".sh",
   "mimetype": "text/x-sh",
   "name": "bash"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
